package algotithm.leetcode.listnode.test24;

import algotithm.leetcode.listnode.ListNode;

/**
 * @author zhouyanxiang
 * @Date 2021-02-2021/2/18-12:27
 * @URL https://leetcode-cn.com/problems/swap-nodes-in-pairs/
 * @Title 24. 两两交换链表中的节点
 */
public class Solution {

    public static void main(String[] args) {
        ListNode head = new ListNode(1);
        ListNode l2 = new ListNode(2);
        ListNode l3 = new ListNode(3);
        ListNode l4 = new ListNode(4);

        head.next = l2;
        l2.next = l3;
        l3.next = l4;
        l4.next = null;

        Solution solution = new Solution();
        ListNode listNode = solution.swapPairs(head);
        while (listNode != null) {
            System.out.print(listNode.val + " ");
            listNode = listNode.next;
        }

    }

    public ListNode swapPairs(ListNode head) {
        if (head == null || head.next == null) {
            return head;
        }
        ListNode next = head.next;
        head.next = swapPairs(next.next);
        next.next = head;
        return next;
    }


}
